package com.fg.cloud.wechar.controller.order;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.Result;
import com.fg.cloud.common.ServiceException;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.UserType;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.reflection.ParamsReflect;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.agency.vo.AgencyUserVo;
import com.fg.cloud.manager.entry.order.OrderCheckEntity;
import com.fg.cloud.manager.entry.order.OrderDetailEntity;
import com.fg.cloud.manager.entry.order.OrderInfoEntity;
import com.fg.cloud.manager.entry.order.vo.OrderInfoVo;
import com.fg.cloud.manager.service.order.OrderCheckService;
import com.fg.cloud.manager.service.order.OrderDetailService;
import com.fg.cloud.manager.service.order.OrderInfoService;
import com.fg.cloud.wechar.controller.BaseWeCharController;

/**
 * Description:手机端订单功能
 * @author liushengchao
 * @date 2017年9月18日上午9:31:23
 *
 */
@Controller
@RequestMapping(value="/wechatorder")
public class WecharOrderController extends BaseWeCharController {
	@Autowired
	private OrderInfoService orderInfoService;
	@Autowired
	private OrderDetailService detailService ; 
	@Autowired
	private OrderCheckService checkService ; 
	/**
	 * Description:查询订单列表
	 * @param req
	 * @param resp
	 * @param search
	 * @param searchCondition
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @author liushengchao
	 * @date  2017年9月18日上午10:26:54
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value="/orderlist",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void findList(HttpServletRequest req , HttpServletResponse resp,String search,
			OrderInfoVo searchCondition,Integer page, Integer rows, String sort, String order,
			String statusStr){
		try {
			AgencyUserVo user = getCurrentAgencyUser();
			PageUtils<BaseEntity> pageinfo = new PageUtils<BaseEntity>(page, rows, sort, order);
			Map<String,Object> condition = ParamsReflect.getParamsByReflect(searchCondition);
			pageinfo.setCondition(condition);
			pageinfo.setFacId(user.getFacId());
			pageinfo.setAgeId(user.getAgeId());
			
			orderInfoService.findList(pageinfo, search, null, null, statusStr,null);

			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, "查询订单成功");
		} catch (Exception e) {
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:订单详情查询
	 * @param req
	 * @param resp
	 * @param id 订单ID
	 * @author liushengchao
	 * @date  2017年9月18日上午11:47:10
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value="/detail",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void detail(HttpServletRequest req , HttpServletResponse resp,Long id,
			String orders,String sort){
		if(id < 1){
			doResult(resp, false, StatusCode.LACKVALUE, null, "订单ID不能为空");
			return ;
		}
		OrderInfoEntity order = orderInfoService.find(id);
		if(order == null ){
			doResult(resp, false, StatusCode.NORETRUNDATA, null, "查询结果为空");
			return ;
		}
		List<OrderDetailEntity> detail = detailService.selectByOrderId(id,orders,sort);
		List<OrderCheckEntity> operatorRecord = checkService.selectByOrderId(id);
		if(detail == null || detail.size() < 0){
			doResult(resp, false, StatusCode.NORETRUNDATA, null, "该订单没有商品信息");
			return ;
		}
		String detailStr = JSON.toJSONString(detail);
		order.setDetails(detailStr);
		order.setListDetails(detail);
		order.setOperatorRecords(operatorRecord);
		doResult(resp, true, StatusCode.FINDSUCCESS, order, "查询成功");
	}
	
	/**
	 * Description:新增订单
	 * @param req
	 * @param resp
	 * @param order
	 * @author liushengchao
	 * @date  2017年9月11日上午10:52:53
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value="/addorder")
	@ResponseBody
	public void addOrder(HttpServletRequest req ,HttpServletResponse resp,String data ){
		try {
			OrderInfoVo order = JSON.parseObject(
					StringEscapeUtils.unescapeHtml4(data), OrderInfoVo.class);
			String type = "addOrder" ;
			Result result  = orderInfoService.AgencyInsertOrder(order,type);
			doResult(resp, result.isSuccess(), result.getCode(), 
					result.getData(), result.getMessage());
		} catch (ServiceException e) {
			doResult(resp, false, e.getCode(), null, e.getMessage());
		} catch (Exception e) {
			doResult(resp, false, StatusCode.EXCEPTIONERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:订单信息保存（不进入正式流程）
	 * @param req
	 * @param resp
	 * @param order  订单信息
	 * @author liushengchao
	 * @date  2017年9月11日下午3:08:43
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value="/saveorder")
	@ResponseBody
	public void saveOrder(HttpServletRequest req ,HttpServletResponse resp,String data){
		try {
			OrderInfoVo order = JSON.parseObject(
					StringEscapeUtils.unescapeHtml4(data), OrderInfoVo.class);
			String type = "save" ;
			Result result  = orderInfoService.AgencyInsertOrder(order,type);
			doResult(resp, result.isSuccess(), result.getCode(), 
					result.getData(), result.getMessage());
		} catch (ServiceException e) {
			doResult(resp, false, e.getCode(), null, e.getMessage());
		} catch (Exception e) {
			doResult(resp, false, StatusCode.EXCEPTIONERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:订单修改
	 * @param req
	 * @param resp
	 * @param order
	 * @author liushengchao
	 * @date  2017年9月11日下午3:18:20
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value="/modify")
	@ResponseBody
	public void modify(HttpServletRequest req ,HttpServletResponse resp,String data){
		OrderInfoEntity order = JSON.parseObject(
				StringEscapeUtils.unescapeHtml4(data), OrderInfoEntity.class);
		if(order==null || order.getOrderStatus() < 9){
			doResult(resp, false, StatusCode.NOPERMISS, order, "订单已进入正式流程，不能修改");
			return ;
		}
		Result result = orderInfoService.AgencyModifyOrder(order);
		doResult(resp, result.isSuccess(), result.getData(), result.getMessage());
	}
}
